﻿using System;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace RibbonEditor.Forms.Publishers
{
    public partial class PublisherPicker : Form
    {
        IOrganizationService innerService;

        public Entity SelectedPublisher { get; set; }

        public PublisherPicker(IOrganizationService service)
        {
            InitializeComponent();

            this.innerService = service;
            this.FillList();
        }

        private void FillList()
        {
            this.lstPublishers.Items.Clear();

            EntityCollection ec = this.RetrievePublishers();

            foreach (Entity solution in ec.Entities)
            {
                ListViewItem item = new ListViewItem(solution["friendlyname"].ToString());
                item.SubItems.Add(solution["uniquename"].ToString());
                item.Tag = solution;

                this.lstPublishers.Items.Add(item);
            }
        }

        private EntityCollection RetrievePublishers()
        {
            try
            {
                QueryExpression qe = new QueryExpression("publisher");
                qe.ColumnSet = new ColumnSet()
                {
                    AllColumns = true
                };

                return this.innerService.RetrieveMultiple(qe);
            }
            catch (Exception error)
            {
                throw new Exception("Error while retrieving publishers: " + error.ToString());
            }
        }

        private void btnNewPublisher_Click(object sender, EventArgs e)
        {
            CreatePublisherDialog cpDialog = new CreatePublisherDialog(this.innerService, null);
            cpDialog.StartPosition = FormStartPosition.CenterParent;

            if (cpDialog.ShowDialog() == DialogResult.OK)
            {
                this.FillList();
            }
        }

        private void lstPublishers_DoubleClick(object sender, EventArgs e)
        {
            if (this.lstPublishers.SelectedItems.Count > 0)
            {
                this.SelectedPublisher = (Entity)this.lstPublishers.SelectedItems[0].Tag;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            if (this.lstPublishers.SelectedItems.Count > 0)
            {
                CreatePublisherDialog cpDialog = new CreatePublisherDialog(this.innerService, (Entity)this.lstPublishers.SelectedItems[0].Tag);
                cpDialog.StartPosition = FormStartPosition.CenterParent;
                cpDialog.ShowDialog();
            }
        }

        private void btnPublisherPickerValidate_Click(object sender, EventArgs e)
        {
            if (this.lstPublishers.SelectedItems.Count > 0)
            {
                this.SelectedPublisher = (Entity)this.lstPublishers.SelectedItems[0].Tag;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Please select a publisher!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnPublisherPickerCancel_Click(object sender, EventArgs e)
        {
            this.SelectedPublisher = null;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
